$(function () {
    function formatDate(time,format='YY-MM-DD hh:mm:ss'){
        var date = new Date(time);

        var year = date.getFullYear(),
            month = date.getMonth()+1,
            day = date.getDate(),
            hour = date.getHours(),
            min = date.getMinutes(),
            sec = date.getSeconds();
        var preArr = Array.apply(null,Array(10)).map(function(elem, index) {
            return '0'+index;
        });

        var newTime = format.replace(/YY/g,year)
            .replace(/MM/g,preArr[month]||month)
            .replace(/DD/g,preArr[day]||day)
            .replace(/hh/g,preArr[hour]||hour)
            .replace(/mm/g,preArr[min]||min)
            .replace(/ss/g,preArr[sec]||sec);

        return newTime;
    }

    //加载指定状态的审核记录
    function ajaxPage(pageNum) {
        $.ajax({
            url: baseurl + "ads",
            type: "POST",
            data: {"page": pageNum},
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function (data) {
                let adManager = new AdManager(data.content, $("tbody"));
                adManager.render(pageNum);
            }
        });
    }

    function AdManager(data, tbody) {
        this.data = data;
        this.tbody = tbody;
        this.render = function (page) {
            if(data == false){
                window.location = "http://localhost:9000/login.html"
            }
            //移除除了第一个隐藏的其他卡片控件
            $(".col-md-4:gt(0)").remove();
            $(".col-xs-12").css('display', 'block');
            //移除全部的页码
            $("#page li").remove();

            if(data.pageTotal > 1){
                for(let i = 1; i <= data.pageTotal; i++){
                    let curPage;
                    if(data.pageIndex == i){
                        curPage = $("<li class='active'><a href='#'>"+ i +"</a></li>");
                    }else{
                        curPage = $("<li><a href='#'>"+ i +"</a></li>");
                    }
                    curPage.click(function(e){
                        ajaxPage(i);
                        e.preventDefault();
                    });
                    $("#page").append(curPage);
                }
            }


            this.tbody.remove();
            this.tbody = $("<tbody></tbody>");
            this.data.items.forEach((ad) => {
                let tr = $("<tr></tr>");
                let td1 = $("<td>" + ad.store.name + "</td>")
                let td2 = $("<td>" + ad.store.address + "</td>")
                let td3 = $("<td>" + ad.adForm.price + "</td>")
                let td4 = $("<td>" + formatDate(new Date(ad.adForm.createTime).getTime(),'YY年MM月DD日') + "</td>")
                let td5 = $("<td>" + formatDate(new Date(ad.adForm.consumeTime).getTime(),'YY年MM月DD日') + "</td>")
                let td6;
                if (ad.user != null) {
                    td6 = $("<td>" + ad.user.username + "</td>")
                } else {
                    td6 = $("<td>" + "无" + "</td>")
                }

                let td7 = $("<td></td>")
                if (ad.adForm.consumeTime == null) {
                    let btn = $("<button class='btn btn-primary'>通过</button>");
                    btn.click(() => {
                        $.ajax({
                            url: baseurl + "ad/confirm",
                            type: "POST",
                            data: {"id": ad.adForm.id, "page": page},
                            xhrFields: {
                                withCredentials: true
                            },
                            success: (data) => {
                                new AdManager(data.content,  $("tbody")).render(page);
                            }
                        });
                    });
                    td7.append(btn);
                } else {
                    td7 = $("<td><span class=\"label label-success\">已通过</span></td>")
                }
                tr.append(td1).append(td2).append(td3).append(td4).append(td5).append(td6).append(td7);
                this.tbody.append(tr);
            });
            $(".table,.table-hover").append(this.tbody);
        };

    }


    //商家自荐审核
    $("#adCheck").click(function (e) {
        $(".treeview,.menu-open").removeClass("menu-open");
        $(".active").removeClass("active");
        $(e.target).parent().addClass("active");
        $.ajax({
            url: baseurl + "ads",
            type: "POST",
            data: {"page": 1},
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function (data) {
                let adManager = new AdManager(data.content, $("tbody"));
                adManager.render(1);
            }
        });

        e.preventDefault();
    });


});

